<?php
include('include/init.php');



$do_array = array('view','viewcomment','addcomment');
if (!in_array($_REQUEST['do'],$do_array))
{
    $_REQUEST['do'] = $do_array[0];
}




if ($_REQUEST['do'] == 'view')
{
    $bbcode = new BBCode();
    $template_array = array('home','news','newsbit');
    $template = new Template($template_array);
    $core->clean(array('page'),'REQUEST');
    $news_per_page = intval($core->get('news_per_page'));
    $start = $news_per_page * intval($core->cleaned('page')) - $news_per_page;
    $end = $start + $news_per_page;

    $query = 'SELECT
                news.newsid,
                news.userid,
                news.title,
                news.text,
                news.time,
                user.username
              FROM `'.TABLEPREFIX.'news` AS news
              LEFT JOIN `'.TABLEPREFIX.'user` AS user USING (userid)
              ORDER BY news.time DESC
              LIMIT :start,:end';
    $core->db->prepare($query);
    $core->db->bind('start',$start,Database::PARAM_INT);
    $core->db->bind('start',$end,Database::PARAM_INT);
    $core->db->execute();
    $array = $core->db->fetchAll();
    $core->db->free();

    $news = '';
    foreach ($array as $value)
    {
        $template->set('newsbit','title',$core->html($value['title']));
        $template->set('newsbit','username',$core->html($value['username']));
        $template->set('newsbit','link',$session->generateURL('user.php',array('userid' => $value['userid'])));
        $template->set('newsbit','time',date($core->parseDate($value['time'])));
        $template->set('newsbit','text',$bbcode->bbcode($value['text']));
        $news .= $template->get('newsbit');
        $template->reset('newsbit');
    }
    
    $template->set('news','news',$news);

    $template->set('home','setting_lang',$language->info('shortname'));
    $template->set('home','pagetitle',$core->get('pagetitle'));
    $template->set('home','content',$template->get('news'));
    $template->out('home');
}





if ($_REQUEST['do'] == 'viewcomment')
{
    $bbcode->BBCode();
    $template_array = array('home','newscomment','newsbit','newscommentbit');
    $template = new Template($template_array);
    $core->clean(array('page','newsid'),'REQUEST');
    $query = 'SELECT
                news.newsid,
                news.userid,
                news.title,
                news.text,
                news.time,
                user.username
              FROM `'.TABLEPREFIX.'news` AS news
              LEFT JOIN `'.TABLEPREFIX.'user` AS user USING (userid)
              WHERE news.newsid=:newsid
              LIMIT 1';
    $core->db->prepare($query);
    $core->db->bind('newsid',$core->cleaned('newsid'),Database::PARAM_INT);
    $core->db->execute();
    $array = $core->db->fetchAll();
    $core->db->free();
    
    if (!$array[0]['newsid'])
    {
        $template->set('error','error',$language->get(''));
        $template->set('home','setting_lang',$language->info('shortname'));
	    $template->set('home','pagetitle',$core->get('pagetitle'));
	    $template->set('home','content',$template->get('error'));
	    $template->out('home');
    }
    else
    {
        $query = 'SELECT
	                news.newsid,
	                news.userid,
	                news.title,
	                news.text,
	                news.time,
	                user.username
	              FROM `'.TABLEPREFIX.'news` AS news
	              LEFT JOIN `'.TABLEPREFIX.'user` AS user USING (userid)
	              WHERE news.newsid=:newsid
	              LIMIT 1';
        $core->db->prepare($query);
	    $core->db->bind('newsid',$core->cleaned('newsid'),Database::PARAM_INT);
	    $core->db->execute();
	    $array = $core->db->fetchAll();
	    $core->db->free();
        $template->set('newsbit','title',$core->html($array[0]['title']));
        $template->set('newsbit','username',$core->html($array[0]['username']));
        $template->set('newsbit','link',$session->generateURL('user.php',array('userid' => $array[0]['userid'])));
        $template->set('newsbit','time',date($core->parseDate($array[0]['time'])));
        $template->set('newsbit','text',$bbcode->bbcode($array[0]['text']));
    
    
	    $newscomment_per_page = intval($core->get('newscomment_per_page'));
        $start = $newscomment_per_page * intval($core->cleaned('page')) - $newscomment_per_page;
        $end = $start + $newscomment_per_page;        
        $query = 'SELECT
	                user.userid,
	                user.username,
	                newscomment.newscommentid,
	                newscomment.userid AS newscommentuserid,
	                newscomment.username AS newscommentusername,
	                newscomment.title AS newscommenttitle
	                newscomment.text,
	                newscomment.time
	              FROM `'.TABLEPREFIX.'newscomment` AS newscomment
	              LEFT JOIN `'.TABLEPREFIX.'user` AS user USING(userid)
	              WHERE newscomment.newscommentid=:newscommentid
	              ORDER BY newscomment.time DESC
	              LIMIT :start,:end';
	    $core->db->prepare($query);
	    $core->db->bind('newsid',$core->cleaned('newsid'),Database::PARAM_INT);
	    $core->db->bind('start',$start,Database::PARAM_INT);
        $core->db->bind('end',$end,Database::PARAM_INT);
        $array = $core->db->fetchAll();
        $core->db->free();
        
        $newcomment = '';
        foreach ($array as $value)
        {
            $template->set('newscommentbit','title',$core->html($value['title']));
	        $template->set('newscommentbit','username',$core->html($value['username']));
	        $template->set('newscommentbit','link',$session->generateURL('user.php',array('userid' => $value['userid'])));
	        $template->set('newscommentbit','time',date($core->parseDate($value['time'])));
	        $template->set('newscommentbit','text',$bbcode->bbcode($value['text']));
	        $newcomment .= $template->get('newscommentbit');
	        $template->reset('newscommentbit');
        }
        
        $template->set('newscomment','news',$template->get('newsbit'));
        $template->set('newscomment','newscomment',$newcomment);
        
        $template->set('home','setting_lang',$language->info('shortname'));
	    $template->set('home','pagetitle',$core->get('pagetitle'));
	    $template->set('home','content',$template->get('newscomment'));
	    $template->out('home');
    }
}





if ($_REQUEST['do'] == 'addcomment')
{
    $template_array = array('home','addnewscomment');
    $template = new Template($template_array);
    $core->clean(array('newsid'),'REQUEST');
    $core->clean(array('title','text'),'POST');
    
    $query = 'SELECT
                news.newsid,
                news.allowcomments
              FROM `'.TABLEPREFIX.'news` AS news
              WHERE news.newsid=:newsid
              LIMIT 1';
    $core->db->prepare($query);
    $core->db->bind('newsid',$core->cleaned('newsid'),Database::PARAM_INT);
    $core->db->execute();
    $array = $core->db->fetchAll();
    $core->db->free();
    
    
    if (!LOGGEDIN)
    {
        $template->set('error','lang_error',$language->get(''));
        $template->set('home','setting_lang',$language->info('shortname'));
	    $template->set('home','pagetitle',$core->get('pagetitle'));
	    $template->set('home','content',$template->get('error'));
	    $template->out('home');
    }
    elseif (!$array[0]['newsid'])
    {
        $template->set('error','lang_error',$language->get(''));
        $template->set('home','setting_lang',$language->info('shortname'));
	    $template->set('home','pagetitle',$core->get('pagetitle'));
	    $template->set('home','content',$template->get('error'));
	    $template->out('home');
    }
    elseif (!$user->get('userpermission','USER_CAN_ADD_NEWSCOMMENT'))
    {
        $template->set('error','lang_error',$language->get(''));
        $template->set('home','setting_lang',$language->info('shortname'));
	    $template->set('home','pagetitle',$core->get('pagetitle'));
	    $template->set('home','content',$template->get('error'));
	    $template->out('home');
    }
    elseif (!$array[0]['allowcomments'])
    {
        $template->set('error','lang_error',$language->get(''));
        $template->set('home','setting_lang',$language->info('shortname'));
	    $template->set('home','pagetitle',$core->get('pagetitle'));
	    $template->set('home','content',$template->get('error'));
	    $template->out('home');
    }
    else
    {
        if ($_POST['submit2'])
        {
            $template->set('addnewscomment','lang_addnewscomment_title',$language->get('addnewscomment_title'));
            $template->set('addnewscomment','lang_addnewscomment',$language->get('addnewscomment'));
            $template->set('addnewscomment','lang_addnewscommenterror_title','');
            $template->set('addnewscomment','lang_addnewscommenterror_text','');
            $template->set('addnewscomment','request_addnewscomment_title','');
            $template->set('addnewscomment','request_addnewscomment_text','');
            
	        $template->set('home','setting_lang',$language->info('shortname'));
		    $template->set('home','pagetitle',$core->get('pagetitle'));
		    $template->set('home','content',$template->get('addnewscomment'));
		    $template->out('home');
        }
        elseif ($_POST['submit'])
        {
            $addcommenterror = FALSE;
            if (!strlen($core->cleaned('title')))
            {
                $template->set('addnewscomment','lang_addnewscommenterror_title',$language->get('addnewscommenterror_title'));
                $addcommenterror = TRUE;
            }
            else
            {
                $template->set('addnewscomment','lang_addnewscommenterror_title','');
            }
            
            $addcomment = FALSE;
            if (!strlen($core->cleaned('text')))
            {
                $template->set('addnewscomment','lang_addnewscommenterror_text',$language->get('addnewscommenterror_text'));
                $addcommenterror = TRUE;
            }
            else
            {
                $template->set('addnewscomment','lang_addnewscommenterror_text','');
            }
            
            
            if ($addcommenterror)
            {
	            $template->set('addnewscomment','lang_addnewscomment_title',$language->get('addnewscomment_title'));
	            $template->set('addnewscomment','lang_addnewscomment',$language->get('addnewscomment'));
	            $template->set('addnewscomment','request_addnewscomment_title',$core->html($core->cleaned('title')));
	            $template->set('addnewscomment','request_addnewscomment_text',$core->html($core->cleaned('text')));
	            
	            $template->set('home','setting_lang',$language->get('shortname'));
	            $template->set('home','pagetitle',$core->get('pagetitle'));
	            $template->set('home','content',$template->get('addnewscomment'));
	            $template->out('home');
            }
            else
            {
                $query = 'INSERT INTO `'.TABLEPREFIX.'newscomment`
                          (
                            title,
                            text,
                            userid,
                            time
                          )
                          VALUES
                          (
                          	:title,
                          	:text,
                          	:userid,
                          	:time
                          )';
                $core->db->prepare($query);
                $core->db->bind('title',$core->cleaned('title'),Database::PARAM_STR);
                $core->db->bind('text',$core->cleaned('text'),Database::PARAM_STR);
                $core->db->bind('userid',$user->get('userid'),Database::PARAM_INT);
                $core->db->bind('time',time(),Database::PARAM_INT);
                $core->db->execute();
                $core->db->free();
                
                $template->set('redirect','url',$session->generateURL('news.php',array('do' => 'viewcomment','newsid' => $array[0]['newsid'])));
	            $template->set('redirect','lang_redirect',$language->get('redirect'));
	            $template->set('redirect','message',$language->get('addnewscomment_successful'));
                
                $template->set('home','setting_lang',$language->get('shortname'));
	            $template->set('home','pagetitle',$core->get('pagetitle'));
	            $template->set('home','content',$template->get('addnewscomment'));
	            $template->out('home');
            }
        }
        else
        {
            $template->set('addnewscomment','lang_addnewscomment_title',$language->get('addnewscomment_title'));
            $template->set('addnewscomment','lang_addnewscomment',$language->get('addnewscomment'));
            $template->set('addnewscomment','lang_addnewscommenterror_title','');
            $template->set('addnewscomment','lang_addnewscommenterror_text','');
            $template->set('addnewscomment','request_addnewscomment_title','');
            $template->set('addnewscomment','request_addnewscomment_text','');
            
	        $template->set('home','setting_lang',$language->info('shortname'));
		    $template->set('home','pagetitle',$core->get('pagetitle'));
		    $template->set('home','content',$template->get('addnewscomment'));
		    $template->out('home');
        }
    }
}



if ($_REQUEST['do'] == 'editcomment')
{
    $template_array = array('home','editnewscomment');
    $template = new Template($template_array);
    $core->clean(array('newscomment'),'REQUEST');
    $core->clean(array('title','text'),'POST');
    
    $query = 'SELECT
                newscomment.newscommentid,
                newscomment.title,
                newscomment.text,
                newscomment.userid,
                news.newsid,
                news.allowcomments
              FROM `'.TABLEPREFIX.'newscomment` AS newscomment
              LEFT JOIN `'.TABLEPREFIX.'news` AS news USING(newsid)
              WHERE newscomment.newscommentid=:newscommentid
              LIMIT 1';
    $core->db->prepare($query);
    $core->db->bind('newscommentid',$core->cleaned('newscommentid'),Database::PARAM_INT);
    $core->db->execute();
    $array = $core->db->fetchAll();
    $core->db->free();
    
    
    if (!LOGGEDIN)
    {
        $template->set('error','lang_error',$language->get(''));
        $template->set('home','setting_lang',$language->info('shortname'));
	    $template->set('home','pagetitle',$core->get('pagetitle'));
	    $template->set('home','content',$template->get('error'));
	    $template->out('home');
    }
    elseif (!$array[0]['newscommentid'])
    {
        $template->set('error','lang_error',$language->get(''));
        $template->set('home','setting_lang',$language->info('shortname'));
	    $template->set('home','pagetitle',$core->get('pagetitle'));
	    $template->set('home','content',$template->get('error'));
	    $template->out('home');
    }
    elseif (($user->get('userid') != $array[0]['userid']) && !$user->get('adminpermission','ADMIN_CAN_EDIT_NEWSCOMMENT'))
    {
        $template->set('error','lang_error',$language->get(''));
        $template->set('home','setting_lang',$language->info('shortname'));
	    $template->set('home','pagetitle',$core->get('pagetitle'));
	    $template->set('home','content',$template->get('error'));
	    $template->out('home');
    }
    elseif (!$user->get('userpermission','USER_CAN_EDIT_NEWSCOMMENT'))
    {
        $template->set('error','lang_error',$language->get(''));
        $template->set('home','setting_lang',$language->info('shortname'));
	    $template->set('home','pagetitle',$core->get('pagetitle'));
	    $template->set('home','content',$template->get('error'));
	    $template->out('home');
    }
    elseif (!$array[0]['allowcomments'])
    {
        $template->set('error','lang_error',$language->get(''));
        $template->set('home','setting_lang',$language->info('shortname'));
	    $template->set('home','pagetitle',$core->get('pagetitle'));
	    $template->set('home','content',$template->get('error'));
	    $template->out('home');
    }
    else
    {
        if ($_POST['submit2'])
        {
            $template->set('editnewscomment','lang_editnewscomment_title',$language->get('addnewscomment_title'));
            $template->set('editnewscomment','lang_editnewscomment',$language->get('addnewscomment'));
            $template->set('editnewscomment','lang_editnewscommenterror_title','');
            $template->set('editnewscomment','lang_editnewscommenterror_text','');
            $template->set('editnewscomment','request_editnewscomment_title','');
            $template->set('editnewscomment','request_editnewscomment_text','');
            
	        $template->set('home','setting_lang',$language->info('shortname'));
		    $template->set('home','pagetitle',$core->get('pagetitle'));
		    $template->set('home','content',$template->get('addnewscomment'));
		    $template->out('home');
        }
        elseif ($_POST['submit'])
        {
            $editcommenterror = FALSE;
            if (!strlen($core->cleaned('title')))
            {
                $template->set('editnewscomment','lang_editnewscommenterror_title',$language->get('addnewscommenterror_title'));
                $editcommenterror = TRUE;
            }
            else
            {
                $template->set('editnewscomment','lang_editnewscommenterror_title','');
            }
            
            $addcomment = FALSE;
            if (!strlen($core->cleaned('text')))
            {
                $template->set('editnewscomment','lang_editnewscommenterror_text',$language->get('addnewscommenterror_text'));
                $editcommenterror = TRUE;
            }
            else
            {
                $template->set('editnewscomment','lang_editnewscommenterror_text','');
            }
            
            
            if ($editcommenterror)
            {
	            $template->set('editnewscomment','lang_editnewscomment_title',$language->get('editnewscomment_title'));
	            $template->set('editnewscomment','lang_editnewscomment',$language->get('editnewscomment'));
	            $template->set('editnewscomment','request_editnewscomment_title',$core->html($core->cleaned('title')));
	            $template->set('editnewscomment','request_editnewscomment_text',$core->html($core->cleaned('text')));
	            
	            $template->set('home','setting_lang',$language->get('shortname'));
	            $template->set('home','pagetitle',$core->get('pagetitle'));
	            $template->set('home','content',$template->get('addnewscomment'));
	            $template->out('home');
            }
            else
            {
                $query = 'UPDATE `'.TABLEPREFIX.'newscomment`
				          SET
                            title=:title,
                            text=:text,
                            userid=:userid
                          WHERE newscommentid=:newscommentid';
                $core->db->prepare($query);
                $core->db->bind('title',$core->cleaned('title'),Database::PARAM_STR);
                $core->db->bind('text',$core->cleaned('text'),Database::PARAM_STR);
                $core->db->bind('userid',$user->get('userid'),Database::PARAM_INT);
                $core->db->bind('newscommentid',$array[0]['newscommentid'],Database::PARAM_INT);
                $core->db->execute();
                $core->db->free();
                
                $template->set('redirect','url',$session->generateURL('news.php',array('do' => 'viewcomment','newsid' => $array[0]['newsid'])));
	            $template->set('redirect','lang_redirect',$language->get('redirect'));
	            $template->set('redirect','message',$language->get('addnewscomment_successful'));
                
                $template->set('home','setting_lang',$language->get('shortname'));
	            $template->set('home','pagetitle',$core->get('pagetitle'));
	            $template->set('home','content',$template->get('addnewscomment'));
	            $template->out('home');
            }
        }
        else
        {
            $template->set('editnewscomment','lang_editnewscomment_title',$language->get('editnewscomment_title'));
            $template->set('editnewscomment','lang_editnewscomment',$language->get('editnewscomment'));
            $template->set('editnewscomment','lang_editnewscommenterror_title','');
            $template->set('editnewscomment','lang_editnewscommenterror_text','');
            $template->set('editnewscomment','request_editnewscomment_title','');
            $template->set('editnewscomment','request_editnewscomment_text','');
            
	        $template->set('home','setting_lang',$language->info('shortname'));
		    $template->set('home','pagetitle',$core->get('pagetitle'));
		    $template->set('home','content',$template->get('addnewscomment'));
		    $template->out('home');
        }
    }
}
?>